package com.zdp.io.bio.test1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author ZhengDp
 * @Date 2024/2/5 23:04
 * 时间服务器的客户端
 */
public class TimeClient {
    public static void main(String[] args) {
        int port = 8888;
        if(args != null && args.length > 0) {
            port = Integer.parseInt(args[0]);
        }
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            // 已经阻塞式发起连接
            socket = new Socket("127.0.0.1",port);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(),true);
            // 客户端先发送消息
            out.println("QUERY TIME ORDER");
            System.out.println("Send order 2 server succeed.");
            // 接收消息
            String response = in.readLine();
            System.out.println("Now is : " + response);

        } catch (IOException e) {
            if(in != null) {
                try {
                    in.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if(out != null) {
                out.close();
                out = null;
            }
            if(socket != null) {
                try {
                    socket.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
        }
    }
}
